package com.erp.module.oem

import android.content.Context
import com.erp.Interface.OnUserDataInterface
import com.erp.bean.OemBean
import com.erp.bean.PhotoBean
import com.erp.contacts.Contacts
import com.erp.contacts.Contacts.BOUNDARY
import com.erp.util.Httputil.*
import org.xutils.http.HttpMethod
import org.xutils.http.RequestParams
import org.xutils.x
import java.io.File

class OemModel {
    /**获取对账拍照列表 */
    fun getOemList(context: Context, page: Int, pagesize: Int,onums : String, onUserDataInterface: OnUserDataInterface) {
        val params = RequestParams(Contacts.OEM_ORDER)
        getparams(context, params)
        setProsess(params)
        params.addBodyParameter("page", page.toString())
        params.addBodyParameter("pagesize", pagesize.toString())
        params.addBodyParameter("searchParams[onums]", onums)
        x.http().get(params, Call(context, onUserDataInterface))
    }
    /**获取对账拍照照片*/
    fun getOemPhoto(context: Context, id: String,onUserDataInterface: OnUserDataInterface) {
        val params = RequestParams(Contacts.OEM_ORDER+"/"+id)
        getparams(context, params)
        setProsess(params)
        x.http().get(params, Call(context, onUserDataInterface))
    }

    /**删除代加工工序*/
    fun deleteOemPhoto(context: Context, id: String,onUserDataInterface: OnUserDataInterface) {
        val params = RequestParams(Contacts.OEM_ORDER+"/"+id)
        addtoken(params)
        getparams(context, params)
        setProsess(params)
        x.http().request(HttpMethod.DELETE,params, Call(context, onUserDataInterface))
    }

    /**上传工序照片 */
    fun postDuizhangPhoto(context: Context,id : String, bean: OemBean.gongxuBean, onUserDataInterface: OnUserDataInterface) {
        var params = RequestParams(Contacts.OEM_ORDER+"/"+id+"/upload_oem_order")
        getparams(context, params)
        addtoken(params)
        params!!.addHeader("ACCEPT", "application/json")
        if(bean.oem_ws_id != ""){
            params.addBodyParameter("ws_id", bean.oem_ws_id, BOUNDARY)
        }
        params.addBodyParameter("workshop_section_name", bean.workshop_section_name, BOUNDARY)
        for (i in bean.listphoto.indices) {
            val photo = bean.listphoto[i]
            if (photo.fid == "") {
                params.addBodyParameter("files[new][$i]", File(photo.uri), BOUNDARY)
            }
        }
        for (i in bean.deletephoto.indices) {
            if (bean.deletephoto[i].fid != "") {
                params.addBodyParameter("files[delete][" + bean.deletephoto[i].fid + "]", bean.deletephoto[i].fid, BOUNDARY)
            }
        }
        x.http().post<String>(params, Call(context, onUserDataInterface))
    }

    /**完成代加工*/
    fun wangongOemPhoto(context: Context, id: String,accomplish_count:String,onUserDataInterface: OnUserDataInterface) {
        val params = RequestParams(Contacts.OEM_ORDER+"/"+id+"/accomplish_oem_order")
        getparams(context, params)
        addtoken(params)
        setProsess(params)
        params.addBodyParameter("accomplish_count", accomplish_count,BOUNDARY)
        params.addBodyParameter("", "",BOUNDARY)
        x.http().post(params, Call(context, onUserDataInterface))
    }
}